import tkinter as tk
import tkinter.messagebox as tms

import customtkinter as ctk

from Attriabte import Color
from Entiy.WordGroup import WordGroup
from View.WindowComponent.BodyFrame.BodyFrame import BodyFrame
from View.WindowComponent.BodyFrame.Remember import RememberWord
from View.WindowComponent.Layout.LayoutSide import LayoutSide
from View.WindowThread.GetSound import play_sound


class ReviewWord(BodyFrame):
    def __init__(
            self,
            layout: LayoutSide,
            wordgroup_name,
            with_scoller=False,
    ):
        self.group_name = wordgroup_name
        self.list_english = WordGroup().get_group_with_chinese(self.group_name)
        self.index = 0
        super().__init__(layout, with_scoller)

    def put_value(self, method):
        """
        放置英语与中文
        :param method: up上一页 down下一页
        :return:
        """
        print(self.list_english)

        def soundPlay():
            text = self.english['text']
            play_sound(text)

        if len(self.list_english) == 0:
            return
        if method != "ignore":
            self.index += 1 if method == 'down' else -1
        if self.index < 0:
            self.index = 0
            tms.showinfo("提示", '这是第一个')
        if self.index > len(self.list_english) - 1:
            self.index = len(self.list_english) - 1
            tms.showinfo("提示", '这是最后一个')

        attribate_basic = {
            "bg": Color.cutkinter
        }

        width, height = .8, .8
        inner_w = .5
        inner_h = .7

        middle_frame = ctk.CTkFrame(self.frame)

        middle_frame.place(
            relwidth=width,
            relheight=height,
            relx=(1 - width) / 2,
            rely=(1 - height) / 2
        )
        inner_frame = tk.Label(middle_frame, bg=Color.cutkinter)
        inner_frame.place(
            relx=(1 - inner_w) / 2,
            rely=(1 - inner_h) / 2,
            relwidth=inner_w,
            relheight=inner_h,
        )

        self.up = tk.Label(
            inner_frame,
            image=self.layout.app.up_arrow,
            width=120,
            height=80,
            **attribate_basic
        )
        self.up.pack(pady=12)

        self.english = tk.Label(inner_frame, text=self.list_english[self.index][0], font=("黑体", 24),
                                **attribate_basic)
        self.english.pack(pady=6)

        self.chinese = tk.Label(inner_frame, text=self.list_english[self.index][1], font=("黑体", 18),
                                **attribate_basic)
        self.chinese.pack(pady=6)

        self.sound: tk.Label = tk.Label(
            inner_frame,
            image=self.layout.app.sound
        )
        self.sound.pack(padx=12)

        self.down = tk.Label(
            inner_frame,
            image=self.layout.app.down_arrow,
            width=120,
            height=80,
            bg=Color.cutkinter
        )
        self.down.pack(pady=12)

        self.up.bind('<Button-1>', lambda e: self.put_value("up"))
        self.down.bind('<Button-1>', lambda e: self.put_value("down"))
        self.sound.bind('<Button-1>', lambda e: soundPlay())

        ctk.CTkButton(
            middle_frame,
            text='开 始',
            text_font=("黑体", 15),
            command=lambda : self.layout.body(
                frame=RememberWord(self.layout,self.group_name, self.ws).get(),
                must_do=True
            )

        ).pack(pady=12)

    def pack(self):
        if self.list_english == []:
            tms.showinfo("错误", f"该词组是空空的")

            raise Exception("就是想报错 怎么着？")
            return

        width, height = .8, .8

        ctk.CTkLabel(self.frame, text="  | 学习 > 复习单词", text_font=('黑体', 20), anchor=tk.W).place(
            relheight=(1 - height) / 2,
            x=0,
            y=0
        )

        self.put_value(method="ignore")
        return
